import requests
import unittest
import app
from api.login import LoginAPI
from utils import common_assert


class Testlogin(unittest.TestCase):
    def setUp(self):
        self.login_api = LoginAPI()
        self.session = requests.Session()

    def tearDown(self):
        if self.session:
            self.session.close()

    def test01_login_success(self):
        response = self.login_api.login(self.session, {"mobile": "13800000002", "password": "123456"})
        print(response.json())
        common_assert(self, response, 200, True, 10000, "操作成功")

        # 提取token信息
        app.TOKEN = "Bearer " + response.json().get("data")
        print(app.TOKEN)
        app.headers_data["Authorization"] = app.TOKEN
        print(app.headers_data)

    def test02_mobile_empty(self):
        response = self.login_api.login(self.session, {"mobile": "", "password": "123456"})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    def test03_password_empty(self):
        response = self.login_api.login(self.session, {"mobile": "13800000002", "password": ""})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    def test04_mobile_less_11(self):
        response = self.login_api.login(self.session, {"mobile": "1380000000", "password": "123456"})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    def test05_mobile_more_11(self):
        response = self.login_api.login(self.session, {"mobile": "138000000023", "password": "123456"})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    def test06_mobile_not_number(self):
        response = self.login_api.login(self.session, {"mobile": "error", "password": "123456"})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    def test07_mobile_not_registered(self):
        response = self.login_api.login(self.session, {"mobile": "13800179002", "password": "123456"})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    def test08_excess_param(self):
        response = self.login_api.login(self.session,
                                        {"mobile": "13800000002", "password": "123456", "parameter": "hahaha"})
        print(response.json())
        common_assert(self, response, 200, True, 10000, "操作成功")

    def test09_no_mobile_param(self):
        response = self.login_api.login(self.session, {"password": "123456"})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    def test10_no_password_param(self):
        response = self.login_api.login(self.session, {"mobile": "13800000002"})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    def test11_no_param(self):
        response = self.login_api.login(self.session, None)
        print(response.json())
        common_assert(self, response, 200, False, 99999, "抱歉，系统繁忙，请稍后重试")


    def test12_error_mobile_param(self):
        response = self.login_api.login(self.session, {"mobiel": "13800179002", "password": "123456"})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

    def test13_error_password_param(self):
        response = self.login_api.login(self.session, {"mobile": "13800179002", "passwd": "123456"})
        print(response.json())
        common_assert(self, response, 200, False, 20001, "用户名或密码错误")

